classdef Analyzer < Instrument
    %ANALYZER Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        reference_lvl
        mode
        freq_center
        freq_span
        reference
    end
    
    methods(Static)
        function test()
            AN = Analyzer(0,20);
            try
                AN.set_mode('NWA');
                AN.set_S_parameter('S11');
                S11 = AN.measure();
                AN.set_S_parameter('S22');
                S22 = AN.measure();
                AN.set_mode('SAN');
                AN.set_spectrum_frequencies(4000,10,3);
                spectrum = AN.measure();
                figure('Name','S11');
                plot(S11(:,1),S11(:,2));
                disp(size(S11));
                figure('Name','S22')
                plot(S22(:,1),S22(:,2));
                figure('Name','spectrum');
                plot(spectrum(:,1),spectrum(:,2));
            catch error
                disp(error);
            end
            AN.delete();
        end
    end
    
    methods
        function this = Analyzer(GPIB_controller, GPIB_address, address)
            this = this@Instrument(GPIB_controller, GPIB_address, address);
            fprintf(this.conn,'SYST:PRES');
            fprintf(this.conn,'SYST:DISP:UPD ON');
        end
        
        function delete(this)
            delete@Instrument(this);
            disp('delete analyzer');
        end
        
        function set_mode(this, mode)
            if strcmp(mode, 'NWA') || strcmp(mode, 'SAN')
                this.mode = mode;
                fprintf(this.conn,['INST:SEL ' this.mode]);
            else
                WrongMode = MException('Analyzer:set_mode','Selected mode does not exist');
                throw(WrongMode);
            end
        end
        
        function results = measure(this)
            if strcmp(this.mode,'SAN')
                fprintf(this.conn,'INIT:CONT OFF');
                fprintf(this.conn,'INIT');
                finish = 0;
                while finish ~= 1
                   fprintf(this.conn,'*OPC?');
                   finish = str2num(fscanf(this.conn));
                end
                fprintf(this.conn,'TRAC? TRACE1');
                power = str2num(fscanf(this.conn));
                freq = linspace(this.freq_center - 0.5*this.freq_span,...
                  this.freq_center + 0.5*this.freq_span, length(power))*1E6;
                results = [freq; power]';
            else
                fprintf(this.conn, 'INIT1:CONT OFF');
                fprintf(this.conn, 'INIT1');
                finish = 0;
                while finish ~= 1
                   fprintf(this.conn,'*OPC?');
                   finish = str2num(fscanf(this.conn));
                end
                fprintf(this.conn, 'CALC1:FORM MLOG');
                fprintf(this.conn, 'CALC1:DATA? FDAT');
                power = str2num(fscanf(this.conn));
                fprintf(this.conn, 'CALC1:DATA:STIM?');
                freq = str2num(fscanf(this.conn));
                results = [freq; power]';
            end
        end
        
        function set_spectrum_frequencies(this, freq_center, freq_span, BW)
            this.freq_center = freq_center;
            this.freq_span = freq_span;
            fprintf(this.conn,'FREQ:CENT %fMHz',freq_center);
            fprintf(this.conn,'FREQ:SPAN %fMHz',freq_span);
            fprintf(this.conn,'BAND:RES %fkHz', BW);
        end
        
        function set_S_parameter(this, S)
            if strcmp(S,'S11') || strcmp(S,'S12') || strcmp(S,'S21') || strcmp(S,'S22');
               fprintf(this.conn,'CALC1:PAR:MEAS ''Trc1'', ''%s''',S);
            else
                WrongS = MException('Analyzer:set_S_parameter','No such S parameter');
                throw(WrongS);
            end
        end
        
        function set_reference_lvl(this, reference_lvl)
            this.reference_lvl = reference_lvl;
            fprintf(this.conn,'DISP:TRAC:Y:RLEV %d', reference_lvl);
        end
        
        function set_reference(this, reference)
            this.reference = reference;
            fprintf(this.conn,'ROSC:SOUR %s', reference);
        end
    end
    
end

